import csv
from datetime import datetime

import matplotlib
import matplotlib.pyplot as plt

# 设置中文字体
matplotlib.rc("font",family='MicroSoft YaHei',weight="bold")

# 1.读取scv文件
filename = 'C:/Users/IT14/Desktop/sitka_weather_2018_simple.csv'

# 1.1.创建阅读器
with open(filename) as filename_list:
    # 1.2.创建阅读器csv.reader()
    reader = csv.reader(filename_list);
    # 1.3.获取表头
    header_name = next(reader)

    # 2.打印数据
    for index,column_value in enumerate(header_name):
        print(index,column_value)

    # 3.获取数据
    TMAX ,TDATE,TMIN= [],[],[]
    for max in reader:
        TMAX.append(int(max[5]))
        TDATE.append(datetime.strptime(max[2],'%Y-%m-%d'))
        TMIN.append(int(max[6]))
    print(TMAX)

# 4.绘图-折线图
fig = plt.figure(dpi=128,figsize=(8,6))
plt1=plt.plot(TDATE,TMAX,c='red',label='最高气温')
plt2=plt.plot(TDATE,TMIN,c='blue',label='最低气温')
for a,b in zip(TDATE,TMAX):
    plt.text(a,b+1,b)
for a,b in zip(TDATE,TMIN):
    plt.text(a,b-2,b)
plt.fill_between(TDATE,TMAX,TMIN,facecolor='blue',alpha=0.1)
fig.autofmt_xdate()
# 5.设置格式
plt.title("2018年广州气温数据",fontsize=24)
plt.xlabel('年-月-日',fontsize=16)
plt.ylabel('气温（度）',fontsize=16)
plt.tick_params(axis='both',labelsize=16)
plt.legend()
plt.savefig('气温折线图.png')
plt.show()
